#
# @lc app=leetcode.cn id=2815 lang=python3
#
# [2815] 数组中的最大数对和
#

# @lc code=start
from typing import List

class Solution:
    def solve(self,x : int) -> int:
        ans = 0
        while x:
            ans = max(ans,x%10)
            x //= 10
        return ans
    def maxSum(self, nums: List[int]) -> int:
        ans = -1
        cnt = {}
        for i,x in enumerate(nums):
            y = self.solve(x) # y是最大数字
            if y in cnt:
                ans = max(ans,x + cnt[y])
            cnt[y] = max(cnt.get(y,0),x)
        return ans
# @lc code=end

